//
//  BKOperationQueue.m
//  XPhoto
//
//  Created by baboy on 2019/6/26.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "BKOperationQueue.h"

@implementation BKOperationQueue
- (id)init{
    if (self = [super init]) {
            [self addObserver:self
                 forKeyPath:@"operationCount"
                    options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld
                    context:nil];
        
    }
    return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:@"operationCount"]) {
//        DLOG(@"################%@,%ld,%ld",self.operations, self.operations.count,self.operationCount);
        if ( self.operationCount == 0 && self.completion) {
            self.completion();
        }
    }else{
        if ([super respondsToSelector:@selector(observeValueForKeyPath:ofObject:change:context:)]) {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    }
}
- (void)dealloc{
    DLOG(@"BKOperationQueue dealloc...");
}
@end
